class Solution {
public:
    vector<vector<int>> ans;
    void dfs(vector<int>& candidates, int& target, int pos, vector<int>& v,
             int sum) {
        if (sum > target)
            return;
        if (sum == target) {
            ans.push_back(v);
            return;
        }
        for (int i = pos; i < candidates.size(); i++) {
            v.push_back(candidates[i]);
            dfs(candidates, target, i, v, sum + candidates[i]);
            v.pop_back();
        }
    }
    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        for (int i = 0; i < candidates.size(); i++) {
            vector<int> v;
            v.push_back(candidates[i]);
            dfs(candidates, target, i, v, candidates[i]);
        }
        return ans;
    }
};